package com.lovo.work.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.lovo.common.model.CutPageBean;
import com.lovo.common.model.WorkBean;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoCutPage;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTitleLabel;
import com.lovo.work.service.IWorkService;
import com.lovo.work.service.impl.WorkServiceImpl;

/**
 * 职位管理主面板
 * @author 白德柯
 *
 */
public class WorkPanel extends JPanel{



	/**职位表格组件*/
	private LovoTable workTable;
	/**窗体组件*/
	private JFrame jf;

	private IWorkService workService = new WorkServiceImpl();

	/**分页对象*/
	private LovoCutPage cutPage;
	public WorkPanel(JFrame jf){
		this.jf = jf;
		this.setLayout(null);
		this.init();
	}
	/**
	 * 初始化
	 *
	 */
	private void init() {
		new LovoTitleLabel("职 位 管 理",this);
		this.initTable();
		this.initButton();
		this.initData();
	}
	/**
	 * 初始化数据
	 */
	public void initData(){
		this.updateTable(1);
	}
	
	

	
	//-------------------------------------------------
	/**
	 * 初始化表格
	 *
	 */
	private void initTable() {
		workTable = new LovoTable(this,
				new String[]{"职位名称","职位描述","资料管理","学校管理","用户管理","统计信息"},
				new String[]{"workName","info","dataPermValue","schoolPermValue","userPermValue","totalPermValue"},//职位实体属性名数组
				"id");//主键属性名
		workTable.setSizeAndLocation(20, 90, 700, 300);
		// 分页
		cutPage = new LovoCutPage(20,400,this) {
			@Override
			public void clickEvent(int arg0) {
				updateTable(arg0);
			}
		};
	}
	
	/**
	 * 初始化按钮
	 *
	 */
	private void initButton() {
		LovoButton addButton = new LovoButton(50,460,"添加职位","image/add.png",this);
		
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new WorkAddDialog(jf, WorkPanel.this);
			}
		});
		
		LovoButton updateButton = new LovoButton(250,460,"修改职位","image/update.png",this);
		
		updateButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new WorkUpdateDialog(jf,0, WorkPanel.this);
			}
			
		});
		

	}
	/**
	 * 更新表格数据
	 */
	private void updateTable(int page){
		CutPageBean<WorkBean> cutPageBean = workService.findByItem(page);
		//更新表格
		workTable.updateLovoTable(cutPageBean.getList());
		if(page == 1){
			cutPage.setTotalPage(cutPageBean.getTotalPage());
		}
	}

}
